package com.security.service;

import com.security.model.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.List;

public class InMemoryUserDetailsService implements UserDetailsService {

    private final List<UserDetails> users;

    public InMemoryUserDetailsService(List<UserDetails> users) {
        this.users = users;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails validUser = null;
        for (UserDetails user : users){
            if (username.equals(user.getUsername())){
                validUser = user;
                break;
            }
        }
        if (validUser == null){
            throw new UsernameNotFoundException("User not found");
        }
        return validUser;
    }
}
